package com.yanqu.road.dao.impl.activity.feast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.feast.data.*;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossFeastPvpUserDataDaoImpl extends TempDataObjectDao<CrossFeastPvpUserData> {

    public static String getCrossFeastPvpUserDataSql = "select * from `t_c_feast_pvp_user_data` where `activityId`=?";

    public Map<Long, CrossFeastPvpUserData> getCrossFeastPvpUserDataMap(int activityId) {
        Map<Long, CrossFeastPvpUserData> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossFeastPvpUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossFeastPvpUserData parse = parse(rs);
                map.put(parse.getUserId(), parse);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String insertCrossFeastPvpUserDataSql = "insert into `t_c_feast_pvp_user_data`(activityId,userId,pveProperty,pvpProperty,placeId,position,transferTime,attackTime,attackedTime,reviveTime,upDemandTime,downDemandTime,lockTarget,updatePropertyTime,totalUseEnergy,updateEnergyTime,reviveCnt,killCnt,protectTime,attackScore,seizeScore,updateScoreTime,battleRecord,roundBattleRecordMap,lastPingPlaceTime,totalKillCnt,pvpAble,apply,enterPlaceList) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(CrossFeastPvpUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getPveProperty())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getPvpProperty())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPlaceId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getPosition())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTransferTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getAttackTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getAttackedTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getReviveTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUpDemandTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getDownDemandTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getLockTarget())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUpdatePropertyTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getTotalUseEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUpdateEnergyTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getReviveCnt()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getKillCnt()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getProtectTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getAttackScore()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getSeizeScore()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUpdateScoreTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getBattleRecord())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getRoundBattleRecordMap())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastPingPlaceTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTotalKillCnt()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isPvpAble()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isApply()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getEnterPlaceList(), ",")));
        if (getDb().executeNoneQuery(insertCrossFeastPvpUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateCrossFeastPvpUserDataSql = "update `t_c_feast_pvp_user_data` set pveProperty=?,pvpProperty=?,placeId=?,position=?,transferTime=?,attackTime=?,attackedTime=?,reviveTime=?,upDemandTime=?,downDemandTime=?,lockTarget=?,updatePropertyTime=?,totalUseEnergy=?,updateEnergyTime=?,reviveCnt=?,killCnt=?,protectTime=?,attackScore=?,seizeScore=?,updateScoreTime=?," +
            "battleRecord=?,roundBattleRecordMap=?,lastPingPlaceTime=?,totalKillCnt=?,pvpAble=?,apply=?,enterPlaceList=? where activityId=? and userId=?";

    @Override
    protected boolean updateData(CrossFeastPvpUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getPveProperty())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getPvpProperty())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPlaceId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getPosition())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTransferTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getAttackTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getAttackedTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getReviveTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUpDemandTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getDownDemandTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getLockTarget())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUpdatePropertyTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getTotalUseEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUpdateEnergyTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getReviveCnt()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getKillCnt()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getProtectTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getAttackScore()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getSeizeScore()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUpdateScoreTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getBattleRecord())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getRoundBattleRecordMap())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastPingPlaceTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTotalKillCnt()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isPvpAble()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isApply()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getEnterPlaceList(), ",")));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateCrossFeastPvpUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected CrossFeastPvpUserData parseData(ResultSet rs) throws SQLException {
        CrossFeastPvpUserData data = new CrossFeastPvpUserData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setPveProperty(JSON.parseObject(rs.getString("pveProperty"), new TypeReference<FeastPveUserProperty>() {
        }));
        data.setPvpProperty(JSON.parseObject(rs.getString("pvpProperty"), new TypeReference<FeastPvpUserProperty>() {
        }));
        data.setPlaceId(rs.getInt("placeId"));
        data.setPosition(JSON.parseObject(rs.getString("position"), new TypeReference<CrossFeastPvpPosition>() {
        }));
        data.setTransferTime(rs.getLong("transferTime"));
        data.setAttackTime(rs.getLong("attackTime"));
        data.setAttackedTime(rs.getLong("attackedTime"));
        data.setReviveTime(rs.getLong("reviveTime"));
        data.setUpDemandTime(rs.getLong("upDemandTime"));
        data.setDownDemandTime(rs.getLong("downDemandTime"));
        if(data.getUpDemandTime() > 0 || data.getDownDemandTime() > 0){
            data.setUpDownDemand(true);
        }
        data.setLockTarget(JSON.parseObject(rs.getString("lockTarget"), CrossFeastPvpLockTarget.class));
        data.setReviveCnt(rs.getInt("reviveCnt"));
        data.setKillCnt(rs.getInt("killCnt"));
        data.setProtectTime(rs.getLong("protectTime"));
        data.setUpdatePropertyTime(rs.getLong("updatePropertyTime"));
        data.setTotalUseEnergy(rs.getInt("totalUseEnergy"));
        data.setUpdateEnergyTime(rs.getLong("updateEnergyTime"));
        data.setBattleRecord(JSON.parseObject(rs.getString("battleRecord"), new TypeReference<CrossFeastPvpBattleRecord>() {
        }));
        data.setRoundBattleRecordMap(JSON.parseObject(rs.getString("roundBattleRecordMap"), new TypeReference<ConcurrentHashMap<Integer, CrossFeastPvpBattleRecord>>() {
        }));
        data.setAttackScore(rs.getLong("attackScore"));
        data.setSeizeScore(rs.getLong("seizeScore"));
        data.setUpdateScoreTime(rs.getLong("updateScoreTime"));
        data.setLastPingPlaceTime(rs.getLong("lastPingPlaceTime"));
        data.setTotalKillCnt(rs.getLong("totalKillCnt"));
        data.setPvpAble(rs.getBoolean("pvpAble"));
        data.setApply(rs.getBoolean("apply"));
        data.setEnterPlaceList(StringUtils.stringToIntegerList(rs.getString("enterPlaceList"), ","));
        return data;
    }
}
